package com.zzptc.sky.translate;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.zzptc.sky.translate.Fragment.MyFragment;
import com.zzptc.sky.translate.Fragment.TheWordFragment;
import com.zzptc.sky.translate.Fragment.TranslateFragment;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public Button translation, the_word, me;
    View lineView,line1View,line2View;
    public List<Fragment> allViews = new ArrayList<Fragment>();
    public ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //TODO textContent = 控件获取的内容并去除空格
        initButton();
        lineView = findViewById(R.id.tob_imageView);
        line1View = findViewById(R.id.tob1_imageView);
        line2View = findViewById(R.id.tob2_imageView);
        line1View.setVisibility(View.INVISIBLE);
        line2View.setVisibility(View.INVISIBLE);
        setViewPager();
    }

    //TODO 初始化顶部3个控件按钮
    private void initButton() {
        translation = (Button) findViewById(R.id.translation);
        the_word = (Button) findViewById(R.id.the_word);
        me = (Button) findViewById(R.id.me);
        //OnClickListener 内部类   对这个类设置事件处理的监听器
        translation.setOnClickListener(this);
        the_word.setOnClickListener(this);
        me.setOnClickListener(this);
    }

    //TODO 为 ViewPager设置内容  加载碎片
    private void setViewPager() {

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        allViews.add(new TranslateFragment());
        allViews.add(new TheWordFragment());
        allViews.add(new MyFragment());
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));

        //TODO 监听页面变动
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
//TODO 页面被选中时   显示需要的下划线
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                       lineView.setVisibility(View.VISIBLE);//如果被选中时   下划线可见
                       line1View.setVisibility(View.INVISIBLE);//不可见
                       line2View.setVisibility(View.INVISIBLE);
                        break;
                    case 1:
                        lineView.setVisibility(View.INVISIBLE);
                        line1View.setVisibility(View.VISIBLE);
                        line2View.setVisibility(View.INVISIBLE);
                        break;
                    case 2:
                        lineView.setVisibility(View.INVISIBLE);
                        line1View.setVisibility(View.INVISIBLE);
                        line2View.setVisibility(View.VISIBLE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
//TODO 顶部控件的点击事件   页面切换
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.translation:
                //3个页面通过setCurrentItem(0)制定第一个页面为当前页面
                viewPager.setCurrentItem(0);
            break;

            case R.id.the_word:
                viewPager.setCurrentItem(1);
            break;

            case R.id.me:
                viewPager.setCurrentItem(2);
            break;
        }
    }
//TODO 适配器就像显示器  写一个适配器来承载组件
    private class MyAdapter extends FragmentPagerAdapter{


        public MyAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            return allViews.get(position);
        }

        @Override
        public int getCount() {
            return allViews.size();
        }
    }
}
